package com.pk.platform.business.common.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.pk.platform.business.charge.dao.ITuitionDao;
import com.pk.platform.business.common.dao.IChildDao;
import com.pk.platform.business.common.dao.IClaDao;
import com.pk.platform.business.common.service.IChildService;
import com.pk.platform.business.common.service.IKindergartenService;
import com.pk.platform.business.common.vo.ChildVO;
import com.pk.platform.domain.common.Child;
import com.pk.platform.domain.common.Cla;
import com.pk.platform.domain.common.Kindergarten;
import com.pk.platform.util.DateConverter;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class ChildServiceImpl implements IChildService {

	private IChildDao childDao;
	
	private ITuitionDao tuitionDao;
	
	private IClaDao claDao;
	
	private IKindergartenService kindergartenService;
	
	public ListPage queryChildren(ChildVO childvo, Pager pager) {
		return childDao.queryChildren(childvo, pager);
	}
	
	public synchronized String addChild(Child child)
	{
		int maxNum = childDao.getMaxNumByKg(child.getKindergarten().getId());
		int nexNum = maxNum + 1;
		String childNum = StringConverter.fillZero(5, nexNum+"");
		child.setChildNum(childNum);
		childDao.add(child);
		childDao.addOperateLog("新增学童" + child.getChildName(), Constant.SUCCESSFUL, child.getKindergarten());
		return Constant.SUCCESS;
	}
	
	public String updateChild(Child child)
	{
		Child c = this.queryChildById(child.getId());
		c.setChildName(child.getChildName());
		c.setCla(child.getCla());
		c.setSex(child.getSex());
		c.setBirthday(child.getBirthday());
		c.setEntranceTime(child.getEntranceTime());
		c.setParent(child.getParent());
		c.setPhone(child.getPhone());
		c.setAddress(child.getAddress());
		c.setResidence(child.getResidence());
		c.setStatus(child.getStatus());
		childDao.addOperateLog("修改学童", Constant.SUCCESSFUL, c.getKindergarten());
		return Constant.SUCCESS;
	}
	
	public boolean delChild(String id)
	{
		int tuitionCount = tuitionDao.queryTuitionCountByChild(id);
		if(tuitionCount == 0)
		{
			Child child = this.queryChildById(id);
			childDao.addOperateLog("删除学童", Constant.SUCCESSFUL, child.getKindergarten());
			childDao.delete(Child.class, id);
			return true;
		}
		return false;
	}
	
	public void batchUpdateClass(String childIds, String classId)
	{
		String[] ids = childIds.split(",");
		Kindergarten kinder = null;
		for(String id : ids)
		{
			Child child = this.queryChildById(id);
			if(kinder == null)
			{
				kinder = child.getKindergarten();
			}
			Cla cla = claDao.get(Cla.class, classId);
			child.setCla(cla);
		}
		childDao.addOperateLog("批量修改学童所属班级", Constant.SUCCESSFUL, kinder);
	}
	
	public void batchUpdateStatus(String childIds, Integer status)
	{
		String[] ids = childIds.split(",");
		Kindergarten kinder = null;
		for(String id : ids)
		{
			Child child = this.queryChildById(id);
			if(kinder == null)
			{
				kinder = child.getKindergarten();
			}
			child.setStatus(status);
		}
		childDao.addOperateLog("批量修改学童是否在读", Constant.SUCCESSFUL, kinder);
	}
	
	public ListPage queryPaySituationByThisMonth(ChildVO childvo, Pager pager)
	{
		return childDao.queryPaySituationByThisMonth(childvo, pager);
	}
	
	public Child queryChildById(String id)
	{
		return childDao.get(Child.class, id);
	}
	
	public InputStream exportChildren(ChildVO childvo)
	{
		// 将OutputStream转化为InputStream
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		putDataOnOutputStream(out, childvo);
		return new ByteArrayInputStream(out.toByteArray());
	}
	
	private void putDataOnOutputStream(OutputStream os, ChildVO childvo) {
		WritableWorkbook workbook = null;
		try {
			workbook = Workbook.createWorkbook(os);
			Kindergarten kinder = kindergartenService.queryKindergartenById(childvo.getKindergartenId());
			List<Map<String, Object>> resultList = childDao.queryChildrenList(childvo);
			WritableFont wf1 = new WritableFont(WritableFont.TIMES, 13,	WritableFont.BOLD);
			WritableCellFormat wcf1 = new WritableCellFormat(wf1);
			wcf1.setAlignment(Alignment.CENTRE);
			wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf1.setWrap(true);
			WritableFont wf2 = new WritableFont(WritableFont.TIMES, 11,	WritableFont.BOLD);
			WritableCellFormat wcf2 = new WritableCellFormat(wf2);
			wcf2.setAlignment(Alignment.CENTRE);
			wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);
			
			WritableSheet sheet = workbook.createSheet("学童信息列表", 0);
			jxl.write.Label lb = null;
			sheet.setColumnView(0, 15);
			sheet.setColumnView(1, 15);
			sheet.setColumnView(2, 15);
			sheet.setColumnView(3, 18);
			sheet.setColumnView(4, 15);
			sheet.setColumnView(5, 15);
			sheet.setColumnView(6, 15);
			sheet.setColumnView(7, 15);
			sheet.setColumnView(8, 20);
			sheet.setColumnView(9, 15);
			sheet.setColumnView(10, 15);
			Map<String, Object> map;
			sheet.mergeCells(0, 0, 10, 1);
			String head = kinder.getKgName() + "学童信息列表";
			lb = new jxl.write.Label(0, 0, head,wcf1);
			sheet.addCell(lb);
			lb = new jxl.write.Label(0, 2, "编号",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(1, 2, "学童姓名",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(2, 2, "性别",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(3, 2, "所属班级",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(4, 2, "生日",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(5, 2, "入学时间",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(6, 2, "家长",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(7, 2, "联系电话",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(8, 2, "地址",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(9, 2, "户籍信息",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(10, 2, "是否在读",wcf2);
			sheet.addCell(lb);
			
			for (int i = 0; i < resultList.size(); i++) {
				int n = i + 3;
				map = resultList.get(i);
				lb = new Label(0, n, map.get("childNum")==null?"":map.get("childNum").toString());
				sheet.addCell(lb);
				lb = new Label(1, n, map.get("childName")==null?"":map.get("childName").toString());
				sheet.addCell(lb);
				lb = new Label(2, n, map.get("sex")==null?"":map.get("sex").toString());
				sheet.addCell(lb);
				lb = new Label(3, n, map.get("className")==null?"":map.get("className").toString());
				sheet.addCell(lb);
				lb = new Label(4, n, map.get("birthday")==null?"":DateConverter.dateToString((Date)map.get("birthday"), DateConverter.FORMAT_ONE));
				sheet.addCell(lb);
				lb = new Label(5, n, map.get("entranceTime")==null?"":DateConverter.dateToString((Date)map.get("entranceTime"), DateConverter.FORMAT_ONE));
				sheet.addCell(lb);
				lb = new Label(6, n, map.get("parent")==null?"":map.get("parent").toString());
				sheet.addCell(lb);
				lb = new Label(7, n, map.get("phone")==null?"":map.get("phone").toString());
				sheet.addCell(lb);
				lb = new Label(8, n, map.get("address")==null?"":map.get("address").toString());
				sheet.addCell(lb);
				lb = new Label(9, n, map.get("residence")==null?"":map.get("residence").toString());
				sheet.addCell(lb);
				
				String statusStr = Constant.YES;
				boolean status = map.get("status") == null ? false : (Boolean)map.get("status");
				if(status == false)
				{
					statusStr = Constant.NO;
				}
				lb = new Label(10, n, statusStr);
				sheet.addCell(lb);
			}
			workbook.write();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			try
			{
				workbook.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	

	public IChildDao getChildDao() {
		return childDao;
	}

	public void setChildDao(IChildDao childDao) {
		this.childDao = childDao;
	}

	public ITuitionDao getTuitionDao() {
		return tuitionDao;
	}

	public void setTuitionDao(ITuitionDao tuitionDao) {
		this.tuitionDao = tuitionDao;
	}

	public IClaDao getClaDao() {
		return claDao;
	}

	public void setClaDao(IClaDao claDao) {
		this.claDao = claDao;
	}

	public IKindergartenService getKindergartenService() {
		return kindergartenService;
	}

	public void setKindergartenService(IKindergartenService kindergartenService) {
		this.kindergartenService = kindergartenService;
	}

}
